import collections
import bisect


class Solution(object):
    def getDistances(self, arr):
        n = len(arr)
        d = collections.defaultdict(list)
        p = {}
        for index, num in enumerate(arr):
            d[num].append(index)
        for key in d:
            d[key].sort()
            m = len(d[key])
            p[key] = [0] * m
            for i in range(len(d[key])):
                if i == 0:
                    p[key][i] = d[key][0]
                else:
                    p[key][i] = p[key][i - 1] + d[key][i]

        ans = [0] * n
        for index, num in enumerate(arr):
            m = len(d[num])
            id = bisect.bisect_left(d[num], index)
            ans[index] = (id + 1) * index - p[num][id] + p[num][-1] - p[num][id] - (m - id - 1) * index
        return ans


data = Solution()
arr = [2, 1, 3, 1, 2, 3, 3]
print(data.getDistances(arr))
